/*
 * Copyright (c) 2019.  鼎捷软件股份有限公司.All rights reserved.
 */

package com.digiwin.dap.middleware.util;

import com.digiwin.dap.middleware.exception.BusinessException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 复制对象
 */
public class ObjectCopyUtil {

    /**
     * 将origin属性注入到destination中
     *
     * @param origin      源对象
     * @param destination 目标对象
     */
    public static void mergeObject(Object origin, Object destination) {

        if (origin == null || destination == null) {
            return;
        }
        if (!origin.getClass().equals(destination.getClass())) {
            return;
        }
        List<Field> fieldList = new ArrayList<>();
        Class tempClass = origin.getClass();
        //当父类为null的时候说明到达了最上层的父类(Object类).
        while (tempClass != null) {
            fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            //得到父类,然后赋给自己
            tempClass = tempClass.getSuperclass();
        }

        for (Field field : fieldList) {
            field.setAccessible(true);
            try {
                Object value = field.get(origin);
                if (null != value && !StringUtils.isEmpty(value)) {
                    field.set(destination, value);
                }
                field.setAccessible(false);
            } catch (IllegalAccessException e) {
                throw new BusinessException("对象合并错误！", e);
            }
        }
    }

    /**
     * 将origin属性注入到destination中
     *
     * @param origin      源对象
     * @param destination 目标对象
     */
    public static void mergeDifferentTypeObject(Object origin, Object destination) {

        if (origin == null || destination == null) {
            return;
        }
        List<Field> originFieldList = getAllFields(origin);
        List<Field> destinationFieldList = getAllFields(destination);

        for (Field origField : originFieldList) {
            for (Field destField : destinationFieldList) {
                if (origField.getName().equals(destField.getName())) {
                    origField.setAccessible(true);
                    destField.setAccessible(true);
                    try {
                        Object value = origField.get(origin);
                        JsonIgnore jsonIgnore = origField.getAnnotation(JsonIgnore.class);
                        if (jsonIgnore == null && value != null && !StringUtils.isEmpty(value)) {
                            destField.set(destination, value);
                        }
                        origField.setAccessible(false);
                        destField.setAccessible(false);
                    } catch (IllegalAccessException e) {
                        throw new BusinessException("对象复制错误", e);
                    }
                }
            }
        }
    }

    /**
     * 获取对象的所有字段（包括父类）
     *
     * @param object 对象
     * @return 字段列表
     */
    private static List<Field> getAllFields(Object object) {
        List<Field> fieldList = new ArrayList<>();
        Class tempClass = object.getClass();
        //当父类为null的时候说明到达了最上层的父类(Object类).
        while (tempClass != null) {
            fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            //得到父类,然后赋给自己
            tempClass = tempClass.getSuperclass();
        }
        return fieldList;
    }
}
